/*
 * Copyright (C) 2016 Baidu, Inc. All Rights Reserved.
 */

/**
 * @file init
 */

Element.prototype.getText = function () {
    if (this.innerText === undefined) {
        return this.textContent;
    } else {
        return this.innerText;
    }
};
Element.prototype.setText = function (a) {
    if (this.innerText === undefined) {
        this.textContent = a || '';
    } else {
        this.innerText = a || '';
    }
};
Element.prototype.getFinalStyle = function (c, b) {
    var a;
    if (window.getComputedStyle) {
        a = window.getComputedStyle(this, null)[c];
    } else {
        a = this.currentStyle[c];
    }
    if (b != null) {
        a = a * b + 'px';
    }
    return a.substring(0, a.toString().length - 2);
};
Element.prototype.mlellipsis = function (h) {
    var g = this.getText();
    var f = this.getAttribute('title');
    if (f == null) {
        this.setAttribute('title', g);
    } else {
        this.setText(f);
    }
    var e = this.getFinalStyle('fontSize');
    var c = parseInt($(this).css('line-height'), 10);
    if (c === 1) {
        c = $(this).css('line-height') * e;
    }
    var a = this.clientHeight;
    if (c === 'norm') {
        c = Number(e * 1.5);
        var b = this.getAttribute('style') || '';
        this.setAttribute('style', b + ';line-height:' + c + 'px');
    } else {
        c = Number(c);
    }
    var d = Math.floor(h * c);
    if (a >= d) {
        g = this.getText();
        while (d * 3 < this.clientHeight) {
            this.setText(g.substring(0, g.length / 2));
            g = this.getText();
        }
        while (d < this.clientHeight) {
            g = this.getText();
            this.setText(g.replace(/(\s)*([a-zA-Z0-9]?|\W)(\.\.\.)?$/, '...'));
        }
    }
};